<?php
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
echo "<title>Search Result</title>";
$key = $_POST["term"];
require_once("scripts/constraint.php");
$xmlfile = SERVERLOCATION."/data/tree.xml";
$doc = DOMDocument::load($xmlfile);
$doc->validate();
$nodes = $doc->getElementsByTagName("node");
$returnIds = array();
$returnNames = array();
foreach($nodes as $node){
	$name = $node->getAttribute("name");
	$id = $node->getAttribute("id");
	if(eregi($key, $node->getAttribute("name")))
	{
		$returnIds[] = $id;
		$returnNames[] = $name;
	}
}
if(count($returnIds)!=0){
	echo "<h3>Search Result</h3>";
	echo "<ul>";
	for($i=0;$i<count($returnIds);$i++){
		echo "<li>[<a href=\"document.php?id=".$returnIds[$i]."\">" . $returnNames[$i] . "</a>]</li>";
	}
	echo "</ul>";
}else{
	echo "<script language=javascript>\n".
			"alert(\"No document's name include: " . $key . ".\");\n".
			"history.go(-1);\n".
			"</script>";
}
?>